// 1. 安装 express-session
const express = require('express');
//2. 导入 express-session 包
const session = require('express-session');

const app = express();
//3. 设置中间件
app.use(session({
    //秘钥 密码  提供加密使用的字符串
    secret: 'afheiowaflwajefoiwjefoifewfwafjwoiajewoia', 
    //是否每次都重新写入 session
    resave: false,
    //是否为每一个请求创建一个 session 对象
    saveUninitialized: false
}));

app.get('/set-session', (request, response) => {
    //4. 写入session  如果登录成功, 写入到了内存中
    request.session.username = 'zhangsan';
    request.session.email = '123@qq.com';

    response.send('登录成功');
});

app.get('/get-session', (request, response) => {
    //4. 读取session 检测用户的登录状态
    console.log(request.session.username);
    console.log(request.session.email);

    response.send('获取用户信息');
});

app.get('/destroy-session', (request, response) => {
    //4. 删除 session
    request.session.destroy(() => {
        response.send('退出登录');
    });
});


app.get('/', (request, response) => {
    response.end('xxx xxx xx');
});

app.listen(80, () => {
    console.log('服务已经启动 80 端口监听中');
});